package com.petrochina.cloudconfig.biz;

import com.petrochina.cloudconfig.domain.Pkg;
import com.petrochina.cloudconfig.mapper.PkgMapper;
import com.petrochina.cloudconfig.util.FileUtil;
import com.petrochina.cloudconfig.util.PkgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;


@Service
public class PkgBiz {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private String key = "pkgs";

    @Value("${my.pkg.path}")
    private String pkgPath;

    @Autowired
    private PkgMapper pkgMapper;


    public List<Pkg> list(String keyWord){
        return pkgMapper.getPkgs(keyWord);
    }

    public boolean addPkg(Pkg pkg, MultipartFile file) {
        String pkgFullName = PkgUtil.getPkgFullName(pkg);
        pkg.setPkgFullName(pkgFullName);
        //redisTemplate.opsForSet().add(key,pkgFullName);
        File pkgFile = PkgUtil.getPkgFile(pkgPath,pkgFullName);
        boolean flag = pkgMapper.addPkg(pkg);
        if(!pkgFile.exists()){
            pkgFile.getParentFile().mkdirs();
        }
        try {
            file.transferTo(pkgFile);

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return flag;


    }

    public boolean  delPkgByFullName(String pkgFullName) {
        boolean flag = pkgMapper.delPkgByFullName(pkgFullName);
        //redisTemplate.opsForSet().remove(key,pkgFullName);
        File pkgFile = PkgUtil.getPkgFile(pkgPath,pkgFullName);
        if(pkgFile.exists()){
            pkgFile.delete();
        }
        return flag;
    }

    public boolean delPkg(int pkgId){
        Pkg pkg = pkgMapper.getPkg(pkgId);
        if(pkg != null){
            boolean flag = pkgMapper.delPkg(pkgId);
            File pkgFile = PkgUtil.getPkgFile(pkgPath,pkg.getPkgFullName());
            if(pkgFile.exists()){
                pkgFile.delete();
            }
            return flag;
        }else{
            return false;
        }


    }

    /**
     *
     * @param response http response
     * @param pkgId
     */
    public void getPkg(HttpServletResponse response, int pkgId) {
        Pkg pkg = pkgMapper.getPkg(pkgId);
        if(pkg != null){
            File pkgFile = PkgUtil.getPkgFile(pkgPath,pkg.getPkgFullName());
            if(pkgFile!=null){
                FileUtil.saveFileToResponse(response,pkgFile);
            }

        }
    }
    /**
     *
     * @param response
     * @param pkgFullName like com.petrochina.a
     */
    public void getPkgByFullName(HttpServletResponse response, String pkgFullName) {
        File pkgFile = PkgUtil.getPkgFile(pkgPath,pkgFullName);
        if(pkgFile!=null){
            FileUtil.saveFileToResponse(response,pkgFile);
        }

    }

    public Pkg getPkgInfoByFullName(String pkgFullName) {
        return pkgMapper.getPkgByFullName(pkgFullName);
    }

    public Pkg getPkgInfo(int pkgId) {

        return pkgMapper.getPkg(pkgId);
    }
}
